from util.util_docx import DocxExtractor
from util.util_pdf import PdfExtractor


# 读取.docx到 dict_matched_paragraphs, list_matched_images
def read_paragraphs_images_docx(path_docx):
    extractor = DocxExtractor(path_docx)
    extractor.extract_paragraphs()
    extractor.extract_images()
    dict_matched_paragraphs = extractor.get_matched_paragraphs()
    list_matched_images = extractor.get_matched_images()
    return dict_matched_paragraphs, list_matched_images


# 读取.pdf到 dict_matched_paragraphs, list_matched_images
def read_paragraphs_images_pdf(path_docx):
    extractor = PdfExtractor(path_docx)
    extractor.extract_paragraphs()
    extractor.extract_images()
    dict_matched_paragraphs = extractor.get_matched_paragraphs()
    list_matched_images = extractor.get_matched_images()
    return dict_matched_paragraphs, list_matched_images


# 解析 pdf 和 docx, 并判断是否解析成功
def process_parse_file(path_file):
    is_parse_ok, dict_matched_paragraphs, list_matched_images = False, None, None
    # 解析 pdf 和 docx
    if path_file.lower().endswith('.docx'):
        dict_matched_paragraphs, list_matched_images = read_paragraphs_images_docx(path_file)  # i56
    elif path_file.lower().endswith('.pdf'):
        dict_matched_paragraphs, list_matched_images = read_paragraphs_images_pdf(path_file)
    # 检查解析是否成功
    if len(dict_matched_paragraphs["6实验结果分析"]["word"]) > 0 \
            and len(dict_matched_paragraphs["7实验结论"]["word"]) > 0 \
            and len(list_matched_images) > 0:
        is_parse_ok = True
    return is_parse_ok, dict_matched_paragraphs, list_matched_images